<?php
    $conn = new mysqli('localhost', 'root', 'root', 'sy2108');

    if ($conn -> connect_error) {
        die('连接错误');
    }

    $u = $_POST['username'];
    $p = $_POST['password'];

    $sql = "SELECT * FROM `user` WHERE `username`='$u' AND `password`='$p'";

    $res = $conn -> query($sql);

    $obj = mysqli_fetch_assoc($res);

    if ($obj) {
        // 后台只能给前端返回一个对象（字符串类型）
        echo json_encode(array(
            "message" => "登录成功",
            "state" => true,
            "token" => "2903j9hf8923rhu2i3huygdsuyfgusyf239ye92ge8wgf7298"
        ));
    } else {
        echo json_encode(array(
            "message" => "账户名或者密码错误",
            "state" => false,
            "token" => "null"
        ));
    }
?>